package com.hzyl.famousreader.view.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hzyl.famousreader.R;
import com.hzyl.famousreader.repository.persistence.entity.Book;
import com.hzyl.famousreader.repository.webservice.model.BookWrapper;

import org.w3c.dom.Text;

import java.util.List;

public class ShopAdapter extends BaseQuickAdapter<BookWrapper, BaseViewHolder> {

  public ShopAdapter(@Nullable List<BookWrapper> data) {
    super(R.layout.item_shop_detail, data);
  }

  @Override
  protected void convert(BaseViewHolder helper, BookWrapper item) {
    if (item.getBooks() != null && item.getBooks().size() > 0) {
      helper.setText(R.id.shop_tv_topic, item.getTopic());

      TextView view1 = helper.getView(R.id.shop_tv_topic);
      view1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
      view1.setTextColor(Color.BLUE);

      Book book = item.getBooks().get(0);

      TextView view2 = helper.getView(R.id.shop_tv_name);
      view2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

      TextView view3 = helper.getView(R.id.shop_tv_summary);
      view3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

      TextView view4 = helper.getView(R.id.shop_tv_author);
      view4.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

      helper.setText(R.id.shop_tv_name, book.getName())
        .setText(R.id.shop_tv_summary, book.getSummary())
        .setText(R.id.shop_tv_author, book.getAuthor());
      Glide.with(mContext).load(book.getCover()).into((ImageView) helper.getView(R.id.shop_iv_cover));



      List<Book> subLists = item.getBooks().subList(1, item.getBooks().size());
      if (subLists != null && subLists.size() > 0) {
        RecyclerView rv = helper.getView(R.id.shop_rv_simple);
        rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        ShopNestedAdapter nestedAdapter = new ShopNestedAdapter(subLists);
        nestedAdapter.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            ARouter.getInstance().build("/book/info").withObject("book", item.getBooks().get(position + 1)).navigation();
          }
        });
        rv.setAdapter(nestedAdapter);
      }
    }
  }
}
